package fr.utbm.lo54.project.control;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sun.org.apache.xerces.internal.xs.StringList;

import fr.utbm.lo54.project.fr.unitaire.JDBCOptionDAO;
import fr.utbm.lo54.project.module.Option;

/**
 * Servlet implementation class AdminServlet
 */
public class AdminServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private JDBCOptionDAO jdbcOption; 
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AdminServlet() {
    	 super();
    	 jdbcOption= new JDBCOptionDAO();       
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 
		Set<Option> listOptions = jdbcOption.getOptionList();
		
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<html>");
		out.println("<body>");
		out.println("<head>");
		out.println("<title>Setting of database !</title>");
		out.println("</head>");
		out.println("<body>");
		//out.println("</body></html>");
	
		out.println("<form name=\"adminpage\" method=\"get\" action=\"AdminServlet\">");
		
		out.println("<h3>add course</h3>");
		out.println("Code <input type=\"text\" name=\"code\"/><br/>");	
		out.println("Title <input type=\"text\" name=\"title\"/><br/>");
		out.println("Date begin <input type=\"text\" name=\"dbegin\"/><br/>");
		out.println("Date end <input type=\"text\" name=\"dend\"/><br/>");
		out.println("Location <input type=\"text\" name=\"location\"/><br/>");
		
		
		out.println("<input type=\"submit\" value=\"Add\"/><br/>");
		String newCours[]=new String[5];
		newCours[0]= request.getParameter("code");
		newCours[1]= request.getParameter("title");
		newCours[2]= request.getParameter("dbegin");
		newCours[3]= request.getParameter("dend");
		newCours[4]= request.getParameter("location");
		
		try {
			addCours(newCours);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		out.println("</form>");
		
		out.println("<form name=\"adminpage\" method=\"get\" action=\"AdminServlet\">");
		out.println("<h3>search of course</h3>");
		out.println("By key word <input type=\"text\" name=\"key_word\"/><br/>");		
			
		out.println("</select><br/>");
		out.println("<input type=\"submit\" value=\"Search\"/>");

		out.println("<h3>List of the Courses</h3>");
		out.println("<table col=\"5\"><tr>");
		out.println("<th></th><th>Code</th><th>Title</th><th>Sessions</th><th>Location</th><th></th></tr>");
		
		
		for(Option o : listOptions) {
			out.println("<tr>");
			out.println("<td><input type=\"checkbox\" name=\"check_option\" value=" + o.getCode()+ "/>");
			out.println("<td>" + o.getCode() + "</td>");
			out.println("<td>" + o.getTitle() + "</td>");
			out.println("<td>" + o.getDate_start() + " to " + o.getDatae_end() + "</td>");
			out.println("<td>" + o.getLocation() + "</td>");
			out.println("<td></td></tr>");
		}
		out.println("</table>");		
		String checkOption[]=request.getParameterValues("check_option");
		
		out.println("<input type=\"submit\" value=\"Delete\"/></form>");
		
		try {
			deleteCours(checkOption);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	private void deleteCours(String[] checkOption) throws SQLException {
		for ( int i= 0 ;i<checkOption.length; i++ ) { 
			String sql="delete from t_course where code='"+checkOption[i]+"'";
			this.DeleteSql(sql);	
		}
	}

	private void addCours(String newCours[]) throws SQLException {		
		String sql="insert into t_course(code,title) values ('"+newCours[0]+"','"+newCours[1]+"')";
		this.InsertSql(sql);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String args[] = new String[4];
		args[0] = request.getParameter("key_word");
		
		String newCours[]=new String[5];
		newCours[0]= request.getParameter("code");
		newCours[1]= request.getParameter("title");
		newCours[2]= request.getParameter("dbegin");
		newCours[3]= request.getParameter("dend");
		newCours[4]= request.getParameter("location");
		
		
	}
	
	private Statement stmt;
	private int rs;
	
	public int InsertSql(String sqli)throws SQLException{
		if(sqli==null||sqli.equals(" "))
			return 0;			
		sqli=sqli.trim();		
		stmt= jdbcOption.con.createStatement (ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
		rs= stmt.executeUpdate(sqli);
		return rs;
	}	
	
	public int DeleteSql(String sqli)throws SQLException{
		if(sqli==null||sqli.equals(" "))
			return 0;			
		sqli=sqli.trim();		
		stmt= jdbcOption.con.createStatement (ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
		rs= stmt.executeUpdate(sqli);
		return rs;
	}
	
	
}
